﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;

namespace VietImage.Models
{
    public partial class Image
    {
        #region Properties
        public City City
        {
            get
            {
                return this.GetCity();
            }
        }

        public Status Status
        {
            get
            {
                return this.GetStatus();
            }
        }

        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public List<DetailImage> Details
        {
            get
            {
                return this.GetDetails();
            }
        }

        public Category Category
        {
            get
            {
                return this.GetCategory();
            }
        }
        #endregion

        #region Methods
        public Image(int id, System.Data.Linq.Binary source, DateTime createdTime, DateTime takenTime, int id_City, int id_User, int id_Status, String author, int id_Category, String imageType)
        {
            this.ID = id;
            this.Source = source;
            this.CreatedTime = createdTime;
            this.TakenTime = takenTime;
            this.ID_City = id_City;
            this.ID_User = id_User;
            this.ID_Status = id_Status;
            this.ImageType = imageType;
            this.ID_Category = id_Category;
            this.Author = author;
        }

        public Image GetNextSameStatusImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID > this.ID && p.ID_Status == this.ID_Status).OrderBy(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetPreviousSameStatusImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID < this.ID && p.ID_Status == this.ID_Status).OrderByDescending(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetNextSameStatusImage(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID > this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Date == date.Date).OrderBy(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetPreviousSameStatusImage(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID < this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Date == date.Date).OrderByDescending(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetNextSameStatusImage(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID > this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Date >= beginDate.Date && p.CreatedTime.Date <= endDate.Date).OrderBy(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetPreviousSameStatusImage(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID < this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Date >= beginDate.Date && p.CreatedTime.Date <= endDate.Date).OrderByDescending(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetNextSameStatusImage(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID > this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Month == month && p.CreatedTime.Year == year).OrderBy(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image GetPreviousSameStatusImage(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID < this.ID && p.ID_Status == this.ID_Status && p.CreatedTime.Month == month && p.CreatedTime.Year == year).OrderByDescending(p => p.ID);
            if (v.ToList().Count > 0)
                return v.First();
            return null;
        }

        public Image Clone()
        {
            Image img = new Image(this.ID, this.Source, this.CreatedTime, this.TakenTime, this.ID_City, this.ID_User, this.ID_Status, this.Author, this.ID_Category, this.ImageType);
            return img;
        }

        public Category GetCategory()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Categories.Where(p => p.ID == this.ID_Category);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public City GetCity()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Cities.Where(p => p.ID == this.ID_City);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Status GetStatus()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Status.Where(p => p.ID == this.ID_Status);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public DetailImage GetDetail(int lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailImages.Where(p => p.ID_Image == this.ID && p.ID_Language == lang);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<DetailImage> GetDetails()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailImages.Where(p => p.ID_Image == this.ID);

            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public int GetNumberOfDownloads(DateTime date)
        {
            return Image.GetNumberOfDownloads(date, this.ID);
        }

        public int GetNumberOfDownloads(int month, int year)
        {
            return Image.GetNumberOfDownloads(month, year, this.ID);
        }

        public int GetNumberOfDownloads(DateTime beginDate, DateTime endDate)
        {
            return Image.GetNumberOfDownloads(beginDate, endDate, this.ID);
        }

        public List<ErrorItem> Download(int id_User, int id_Action, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.DownloadImage(id_User, this.ID, id_Action);


            if (errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static Image GetImage(int id)
        {
            Image img = new Image();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var v = _db.Images.Where(p => p.ID == id);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public static Image Parse(System.Object value)
        {
            Image temp = null;
            try
            {
                temp = (Image)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static int GetNumberOfDownloads(DateTime date, int id_Image)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date == date.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_Image == id_Image
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfDownloads(int month, int year, int id_Image)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date.Month == month && limg.ActionTime.Year == year && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_Image == id_Image
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfDownloads(DateTime beginDate, DateTime endDate, int id_Image)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_Image == id_Image
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfDownloadsByUser(DateTime date, int id_User)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date == date.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_User == id_User
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfDownloadsByUser(int month, int year, int id_User)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date.Month == month && limg.ActionTime.Year == year && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_User == id_User
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfDownloadsByUser(DateTime beginDate, DateTime endDate, int id_User)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ID_User == id_User
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfDownloads(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date == date.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfDownloads(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date.Month == month && limg.ActionTime.Year == year && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfDownloads(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfUploads(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date == date.Date && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfUploads(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date.Month == month && limg.ActionTime.Year == year && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static int GetAllNumberOfUploads(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate.Date && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfUploads(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date == date.Date && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfUploads(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date.Month == month && limg.ActionTime.Year == year && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static int GetNumberOfUploads(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var temp = from limg in _db.LogImages
                       where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate.Date && limg.ID_Action == 10
                       select limg;

            return temp.Count();
        }

        public static List<Image> GetListDownloadedImage(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = (from img in _db.Images
                     join limg in _db.LogImages on img.ID equals limg.ID_Image
                     where limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9
                     select img);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListDownloadedImage(DateTime date, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = (from img in _db.Images
                     join limg in _db.LogImages on img.ID equals limg.ID_Image
                     where (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9) && limg.ActionTime.Date == date.Date
                     select img);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListUploadedImage(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = (from img in _db.Images
                     join limg in _db.LogImages on img.ID equals limg.ID_Image
                     where limg.ID_Action == 10
                     select img);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count(); ;
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListUploadedImage(DateTime date, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = (from img in _db.Images
                     join limg in _db.LogImages on img.ID equals limg.ID_Image
                     where limg.ID_Action == 10 && limg.ActionTime.Date == date.Date
                     select img);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListUploadedImages(DateTime date, int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.CreatedTime.Date == date.Date);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListUploadedImages(int month, int year, int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.CreatedTime.Date.Month == month && p.CreatedTime.Year == year);

            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListUploadedImages(DateTime beginDate, DateTime endDate, int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.CreatedTime.Date >= beginDate.Date && p.CreatedTime <= endDate.Date);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetList(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images;
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetList(DateTime uploadTime, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.CreatedTime.Date == uploadTime.Date);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotAvailableByUploader(int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Status != 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailableByUploader(int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Status == 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailableByUploader(int id_User, int count, int from, out int n, DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Status == 1 && p.CreatedTime.Date == date.Date);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailableByUploader(int id_User, int count, int from, out int n, DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Status == 1 && p.CreatedTime.Date >= beginDate.Date && p.CreatedTime <= endDate.Date);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailableByUploader(int id_User, int count, int from, out int n, int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Status == 1 && p.CreatedTime.Date.Month == month && p.CreatedTime.Date.Year == year);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailable(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status == 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotAvailable(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status != 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotPublish(int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status == 9);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailable(int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status == 1 && p.ID_User == id_User);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotAvailable(int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status != 1 && p.ID_User == id_User);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotPublish(int id_User, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_Status == 9 && p.ID_User == id_User);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListAvailable(int id_User, int id_Category, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Category == id_Category && p.ID_Status == 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotAvailable(int id_User, int id_Category, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Category == id_Category && p.ID_Status != 1);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        public static List<Image> GetListNotPublish(int id_User, int id_Category, int count, int from, out int n)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Images.Where(p => p.ID_User == id_User && p.ID_Category == id_Category && p.ID_Status == 9);
            n = 0;
            if (v.Count() == 0)
                return null;
            n = v.Count();
            return v.OrderByDescending(p => p.ID).Skip(from).Take(count).ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Image img)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (img.Source == null)
                errorList.Add(new ErrorItem("Source", "Source is required."));
            if (img.Author == "")
                errorList.Add(new ErrorItem("Author", "Author is required"));
            if (img.ID_City < 1)
                errorList.Add(new ErrorItem("ID_City", "City is required"));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Image ii, int id_User, int id_Category, int isPrimary, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ii);
            if (errorList.Count == 0)
            {
                Nullable<Int32> id_Image = new int();
                int errorMess = _db.InsertImage(ref id_Image, id_User, ii.Source, ii.ImageType, ii.TakenTime, ii.ID_City, id_Category, ii.Author);
                if (errorMess == 1)
                {
                    ii.ID = id_Image.Value;
                }

                if (errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Image ic, int id_User, int id_Lang, int isChangeSource)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Image oi = _db.Images.Single(p => p.ID == ic.ID);
                String content = "";
                content = oi.TakenTime + "_" + oi.ID_City + "_" + oi.ID_User + "_" + oi.ID_Status + "_" + oi.Author + "_" + oi.ImageType;
                int errorMess = _db.ChangeImage(id_User, ic.ID, ic.Source, ic.TakenTime, ic.ImageType, ic.ID_City, ic.ID_Status, ic.Author, oi.Source, content, 0, 0, isChangeSource);

                if (errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Image ic, int id_User, int id_Log, int id_Lang, int isChangeSource)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                int errorMess = _db.ChangeImage(id_User, ic.ID, ic.Source, ic.TakenTime, ic.ImageType, ic.ID_City, ic.ID_Status, ic.Author, null, "", id_Log, 1, isChangeSource);

                if (errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Image, int id_User, int deep, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.DeleteImage(id_User, id_Image, deep);

            if (errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Search

        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID" || kvp.Key == "ID_City" || kvp.Key == "ID_Author" || kvp.Key == "ID_Status" || kvp.Key == "ID_User")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "TakenTime" || kvp.Key == "CreatedTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += kvp.Key + " = #" + kvp.Value.ToString() + "#";
                }
            }

            if (parametersList.ContainsKey("CreatedbeginDate") && parametersList.ContainsKey("CreatedendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["CreatedendDate"].ToString()).ToString("yyyy-MM-dd") + " , CreatedTime) >= 0";
                query += " DATEDIFF(day, CreatedTime , " + DateTime.Parse(parametersList["CreatedbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }
            if (parametersList.ContainsKey("TakenbeginDate") && parametersList.ContainsKey("TakenendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["TakenendDate"].ToString()).ToString("yyyy-MM-dd") + " , TakenTime) >= 0";
                query += " DATEDIFF(day, TakenTime , " + DateTime.Parse(parametersList["TakenbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }

            return query;
        }

        public static System.Object GetImage(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Image>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        #endregion

    }
}